<route lang="json5">
{
	style: {
		navigationBarTitleText: '修改账号密码',
	},
}
</route>
<template>
	<view class="page">
		<wd-form ref="form" class="form" :model="model">
			<wd-input
				label="账号"
				label-width="280rpx"
				prop="username"
				clearable
				v-model="model.username"
				placeholder="请输入账号"
				:rules="[{ required: true, message: '请输入账号' }]"
			/>
			<wd-cell-group :border="true">
				<wd-input
					label="新密码"
					label-width="280rpx"
					prop="password"
					show-password
					clearable
					v-model="model.password"
					placeholder="请输入新密码"
					:rules="[{ required: true, message: '请输入新密码' }]"
				/>
			</wd-cell-group>
			<view class="footer">
				<wd-button type="primary" size="large" @click="handleSubmit" block>保存</wd-button>
			</view>
		</wd-form>
	</view>
</template>
<script setup>
import Head from '@/components/head.vue'
import { useUserStore } from '@/store'
import { http } from '@/utils/http'

const form = ref(null)
const userStore = useUserStore()

const model = reactive({
	password: '',
	username: '',
})

const time = ref(0)

onShow(async () => {
	await userStore.getUserInfo()

	model.username = userStore.userInfo.username
})

const handleSubmit = async () => {
	form.value
		.validate()
		.then(async ({ valid, errors }) => {
			if (valid) {
				let res = await http({
					url: '/Usersd/setPassword',
					method: 'post',
					data: model,
				})

				if (res.code == 200) {
					uni.showToast({
						title: res.message,
						duration: 2000,
						icon: 'none',
						complete: () => uni.navigateBack(),
					})
				}
			}
		})
		.catch((error) => console.log('error', error))
}
</script>
<style lang="scss" scoped>
.form {
	width: 710rpx;

	display: flex;
	flex-flow: column nowrap;
	gap: 24rpx;
	padding: 24rpx 0;
}
</style>
